package cn.edu.tju.wallet.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.wallet.dto.VipLevelDTO;
import cn.edu.tju.wallet.service.VipLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * VIP等级管理控制器
 * 提供VIP等级的CRUD操作和用户VIP等级管理功能
 */
@RestController
@RequestMapping("/api/rule/vip")
public class VipLevelController {

    @Autowired
    private VipLevelService vipLevelService;

    /**
     * 创建VIP等级
     */
    @PostMapping("/create")
    public HttpResult<VipLevelDTO> create(@RequestBody VipLevelDTO dto) {
        try {
            VipLevelDTO created = vipLevelService.createVipLevel(dto);
            return HttpResult.success(created);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    /**
     * 更新VIP等级
     */
    @PutMapping("/update/{id}")
    public HttpResult<VipLevelDTO> update(@PathVariable Long id, @RequestBody VipLevelDTO dto) {
        try {
            VipLevelDTO updated = vipLevelService.updateVipLevel(id, dto);
            return HttpResult.success(updated);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    /**
     * 删除VIP等级
     */
    @DeleteMapping("/delete/{id}")
    public HttpResult<Void> delete(@PathVariable Long id) {
        try {
            vipLevelService.deleteVipLevel(id);
            return HttpResult.success(null);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    /**
     * 查询所有VIP等级
     */
    @GetMapping("/list")
    public HttpResult<List<VipLevelDTO>> list() {
        try {
            List<VipLevelDTO> vipLevels = vipLevelService.getAllVipLevels();
            return HttpResult.success(vipLevels);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    /**
     * 根据ID获取VIP等级
     */
    @GetMapping("/{id}")
    public HttpResult<VipLevelDTO> getById(@PathVariable Long id) {
        try {
            VipLevelDTO vipLevel = vipLevelService.getVipLevelById(id);
            return HttpResult.success(vipLevel);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    /**
     * 根据等级数值获取VIP等级
     */
    @GetMapping("/level/{level}")
    public HttpResult<VipLevelDTO> getByLevel(@PathVariable Integer level) {
        try {
            return vipLevelService.getVipLevelByLevel(level)
                    .map(HttpResult::success)
                    .orElse(HttpResult.success(null));
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    /**
     * 获取所有激活的VIP等级
     */
    @GetMapping("/active")
    public HttpResult<List<VipLevelDTO>> getActive() {
        try {
            List<VipLevelDTO> activeLevels = vipLevelService.getAllActiveVipLevels();
            return HttpResult.success(activeLevels);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    /**
     * 初始化默认VIP等级
     * 仅在系统首次启动时调用
     */
    @PostMapping("/init")
    public HttpResult<Void> initDefault() {
        try {
            vipLevelService.initDefaultVipLevels();
            return HttpResult.success(null);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }
}
